﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using Facade;
using IronOne.SalesOne.BusinessObjects;

namespace IronOne.SalesOne.SalesOneWeb.SalesActivity
{
    public partial class UpdateRemark : SOSecurityPage
    {
        string dates;
        string SOCode;
        StructureFacade facade;

        protected void Page_Load(object sender, EventArgs e)
        {
            facade = new StructureFacade();
            dates = Request.QueryString["dates"];
            SOCode = Request.QueryString["SOCode"];            
            
            string[] arrDates=dates.Split(new char[] {','});
            if (dates.Split(new char[] { ',' }).Length > 1)
            {
                lblMsg.Text = " Update Remarks - SC : " + SOCode;                
            }
            else
            {
                lblMsg.Text = " Update Remarks - Date: " + dates.Split(new char[] {'.'})[0]+ " SC : "+SOCode;
            }

            //Response.Write();

            if (string.IsNullOrEmpty(dates))
                Response.Redirect("~/SalesActivity/ActivityPlan.aspx");

            if (!Page.IsPostBack)
            {
                try
                {
                    ValidateUserCredentials(Request.Url.LocalPath);

                    if (!(dates.IndexOf(',') > 0))
                    {
                        ActivityRemark remark = facade.GetRemarkForSO(SOCode, DateTime.Parse(dates.Split('.')[0]));
                        if (remark != null)
                            taUpdateRemark.Text = remark.Remark;
                    }
                }
                catch
                {
                    MassageSummery("IIS514");
                }
            }
        }
        
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/SalesActivity/ActivityPlan.aspx?socode=" + SOCode + "&from=" + Request.QueryString["from"] + "&to=" + Request.QueryString["to"]);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                List<ActivityRemark> updateList = new List<ActivityRemark>();
                List<ActivityRemark> insertList = new List<ActivityRemark>();
                foreach (string date in dates.Split(','))
                {
                    ActivityRemark remark = new ActivityRemark { Date = DateTime.Parse(date.Split('.')[0]), SOCode = SOCode, Remark = taUpdateRemark.Text };
                    if (date.Split('.')[1] == "1")
                        insertList.Add(remark);
                    else
                        updateList.Add(remark);

                }
                facade.InsertOrUpdateRemark(updateList, insertList);

                Response.Redirect("~/SalesActivity/ActivityPlan.aspx?socode=" + SOCode + "&from=" + Request.QueryString["from"] + "&to=" + Request.QueryString["to"]);

            }
            catch
            {
                MassageSummery("IIS514");
            }
        }
    }
}
